package dan.eyt.parser;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import tv.adap.http.HttpConnectionCommonsImpl;
import tv.adap.http.HttpRequest;
import tv.adap.http.HttpResponse;

public class Parser {

	public static final String url = "http://www.dersverilir.net/ara/?city=34&state=429&lesson=4&level=9&page=2"; 
	private HttpConnectionCommonsImpl httpClient; 
	
	public void parse(){
		HttpRequest request = new HttpRequest(url);
		HttpResponse response = httpClient.get(request);
		try {
			System.err.println(parseUsernames(response.getResponseAsString()));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Collection<String> parseUsernames(String body){
		Collection<String> usernames = new ArrayList<String>(); 
		Pattern pattern = Pattern.compile("(username=\"(.*?)\")");
		Matcher matcher = pattern.matcher(body);
		while(matcher.find()){
			usernames.add(matcher.group(2)); 
		}
		return usernames; 
	}
	
	public static void main(String[] args){
		HttpRequest request = new HttpRequest(url);
	}

	public void setHttpClient(HttpConnectionCommonsImpl httpClient) {
		this.httpClient = httpClient;
	}

}
